package com.wwm.petadopt;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import cn.bmob.v3.Bmob;

public class MainActivity extends Activity
{
	private static android.app.FragmentManager fragmentManager;
	@Override
	public void onResume()
	{
		super.onResume();
		FragmentTransaction ft = null;
		String fragmentName = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-1).getName();
		switch (fragmentName)
		{
			case "homepageFragment":

				ActionBar ab = getActionBar();
				if (ab != null)
				{
					View actionbarview=getLayoutInflater().inflate(R.layout.actionbar,null);
					((TextView)actionbarview.findViewById(R.id.text_actionbar)).setText(R.string.homepage);

					ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
					ab.setCustomView(actionbarview);
					Log.i("actionbar", "AccountFragment");
				}

				if (fragmentManager.findFragmentByTag("homepageFragment") != null && fragmentManager.findFragmentByTag("homepageFragment").isVisible())
				{
					return;
				}

				break;
			case "classificationFragment":
				popAllFragmentsExceptTheBottomOne();
				ft = fragmentManager.beginTransaction();
				//ft.setCustomAnimations(R.anim.slide_in_left, 0, 0, R.anim.slide_out_left).show(fragmentManager.findFragmentByTag("homepageFragment"));

				ft.hide(fragmentManager.findFragmentByTag("homepageFragment"));

				ClassificationFragment classificationFragment = new ClassificationFragment();
				ft.add(R.id.layout_fragment_space, classificationFragment, "classificationFragment");
				ft.addToBackStack("classificationFragment");

				ft.commit();

				break;

			case  "accountFragment":
				popAllFragmentsExceptTheBottomOne();

				ft = fragmentManager.beginTransaction();
				ft.hide(fragmentManager.findFragmentByTag("homepageFragment"));
				AccountFragment accountFragment = new AccountFragment();
				ft.add(R.id.layout_fragment_space, accountFragment, "accountFragment");
				ft.addToBackStack("accountFragment");
				ft.commit();

				break;
			default:
				break;
		}


	}
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		super.onCreate(savedInstanceState);

		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
		ActionBar ab = getActionBar();
		if (ab != null)
		{
			View actionbarview=getLayoutInflater().inflate(R.layout.actionbar,null);
			((TextView)actionbarview.findViewById(R.id.text_actionbar)).setText(R.string.homepage);

			ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
			ab.setCustomView(actionbarview);
			Log.i("actionbar", "AccountFragment");
		}
		setContentView(R.layout.activity_main);
		fragmentManager = getFragmentManager();
		initView();
		addRadioGroupListener();
		Bmob.initialize(this, "af01e2fbdf7674a51ee0624c3dfcc586");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if(id == R.id.action_settings)
		{
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed()
	{

		if(fragmentManager.findFragmentByTag("homepageFragment") != null && fragmentManager.findFragmentByTag("homepageFragment").isVisible())
		{
			MainActivity.this.finish();
		}

		else
		{
			//popAllFragmentsExceptTheBottomOne();
			String fragmentName = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-2).getName();
			//Log.i("stackstack", fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-2).getName());
			fragmentManager.popBackStack();

			ActionBar ab = getActionBar();
			if (ab != null)
			{
				View actionbarview = getLayoutInflater().inflate(R.layout.actionbar,null);

				int titleID = 0;
				switch (fragmentName)
				{
					case "homepageFragment":
						titleID = R.string.homepage;
						break;
					case "classificationFragment":
						titleID = R.string.classification;
						break;
					case "accountFragment":
						titleID = R.string.account;
						break;
					case "moreFragment":
						titleID = R.string.more;
						break;
					case "detailFragment":
						titleID = R.string.more;
						break;
					case "registerFragment":
						titleID = R.string.register;
						break;
					case "loginFragment":
						titleID = R.string.login;
						break;
					default:
						break;
				}

				((TextView)actionbarview.findViewById(R.id.text_actionbar)).setText(titleID);

				ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
				ab.setCustomView(actionbarview);
				Log.i("actionbar", "AccountFragment");
			}


			//super.onBackPressed();
		}
	}

	public void initView()
	{
		HomepageFragment homepageFragment = new HomepageFragment();
		FragmentTransaction fragmentTraction = fragmentManager.beginTransaction();
		fragmentTraction.add(R.id.layout_fragment_space, homepageFragment, "homepageFragment");
		fragmentTraction.addToBackStack("homepageFragment");
		fragmentTraction.commit();
	}

	public void addRadioGroupListener()
	{
		RadioGroup navigatorRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_navigator);
		navigatorRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			FragmentTransaction ft = null;

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				switch (checkedId)
				{
					case R.id.radiobutton_homepage:
						group.clearCheck();
						ActionBar ab = getActionBar();
						if (ab != null)
						{
							View actionbarview=getLayoutInflater().inflate(R.layout.actionbar,null);
							((TextView)actionbarview.findViewById(R.id.text_actionbar)).setText(R.string.homepage);

							ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
							ab.setCustomView(actionbarview);
							Log.i("actionbar", "AccountFragment");
						}

						if (fragmentManager.findFragmentByTag("homepageFragment") != null && fragmentManager.findFragmentByTag("homepageFragment").isVisible())
						{
							return;
						}
						popAllFragmentsExceptTheBottomOne();


						break;
					case R.id.radiobutton_classification:
						group.clearCheck();
						popAllFragmentsExceptTheBottomOne();
						ft = fragmentManager.beginTransaction();
						//ft.setCustomAnimations(R.anim.slide_in_left, 0, 0, R.anim.slide_out_left).show(fragmentManager.findFragmentByTag("homepageFragment"));

						ft.hide(fragmentManager.findFragmentByTag("homepageFragment"));

						ClassificationFragment classificationFragment = new ClassificationFragment();
						ft.add(R.id.layout_fragment_space, classificationFragment, "classificationFragment");
						ft.addToBackStack("classificationFragment");

						ft.commit();

						break;

					case R.id.radiobutton_account:
						popAllFragmentsExceptTheBottomOne();
						group.clearCheck();
						ft = fragmentManager.beginTransaction();
						ft.hide(fragmentManager.findFragmentByTag("homepageFragment"));
						AccountFragment accountFragment = new AccountFragment();
						ft.add(R.id.layout_fragment_space, accountFragment, "accountFragment");
						ft.addToBackStack("accountFragment");
						ft.commit();

						break;
					default:
						break;
				}

			}
		});
	}

	public static void popAllFragmentsExceptTheBottomOne()
	{
		for (int i = 0, count = fragmentManager.getBackStackEntryCount() - 1; i < count; i++)
		{
			Log.i("www-fragment0", Integer.toString(i));
			fragmentManager.popBackStack();
		}
	}
}
